package com.zhp.service.impl;

import com.zhp.dao.ArticleDao;
import com.zhp.dao.FavoriteDao;
import com.zhp.model.Article;
import com.zhp.model.Favorite;
import com.zhp.model.User;
import com.zhp.service.ArticleService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArticleServiceImpl implements ArticleService {

    private ArticleDao articleDao = new ArticleDao();
    private FavoriteDao favoriteDao = new FavoriteDao();
    @Override
    public List<Map<String, Object>>  getArticles(String uId,int currentPage, String name) {
        return articleDao.findArticles(uId,currentPage,name);
    }

    @Override
    public List<Map<String, Object>>  getMyArticles(String uId,int currentPage, String name) {
        return articleDao.findMyArticles(uId,currentPage,name);
    }

    @Override
    public long getArticleCount(String name) {
        return articleDao.getArticleTotal(name);
    }

    @Override
    public long getMyArticleCount(String uid,String name) {
        return articleDao.getMyArticleTotal(uid,name);
    }

    @Override
    public Article findByid(String id) {
        return articleDao.findById(Integer.parseInt(id));
    }

    @Override
    public Number getfavorite(String id) {
        return articleDao.getFavoriteCount(id);
    }

    @Override
    public List<User> getUserFavorite(String id) {
        return articleDao.getUserFavoriteByIds(id);
    }

    @Override
    public int addArticle(Article article) {
       return articleDao.save(article);
    }

    @Override
    public int addFavorite(Favorite favorite) {
        return favoriteDao.save(favorite);
    }

    @Override
    public int delFavorite(String uId, String aId) {
        Map<String, Object> map = new HashMap<>();
        map.put("u_id",uId);
        map.put("a_id",aId);
        List<Favorite> favorites = favoriteDao.findByTAnd(map);
        return favoriteDao.deleteById(favorites.get(0).getId());
    }

    @Override
    public int update(Article article) {
        return articleDao.update(article);
    }
}
